<?php
/**
 * Created by PhpStorm.
 * User: wei
 * Date: 2016/5/25
 * Time: 15:55
 */
namespace Admin\Controller;

use Think\Controller;

class MasterManageController extends CommonController
{
    protected function _initialize()
    {
        parent::_initialize();
        $this->breadcrumb1 = '财务管理';
        $this->breadcrumb2 = '师傅佣金和奖励管理';
        $this->url = U('MasterManage/index');
        $this->sqlname='mastermanage'.session('user_auth')['uid'];
    }

    public function index()
    {
if($_POST){
    $time1=I('post.time1');
    $time2=I('post.time2');
    $order_id=I('post.order_id');
    $phone=I('post.phone');
    $type=I('post.type');
    $where='';
    if($time1 && $time2){
        $date1=strtotime($time1);
        $date2=strtotime($time2);
        $where=" and ro.pay_time>$date1 and ro.pay_time<$date2";
    }
    if($order_id){
        $where.=" and ro.repair_order_num_alias=$order_id";
    }
    if($phone){
        $where.=" and u.phone=$phone";
    }
    if($type==1){
        $where.=" and mo.money>0";
    }elseif($type==2){
        $where.=" and mo.money<0";
    }
}
        $countsql = "select count(*) sum from zsf_repair_order ro left join zsf_rewards_money mo on ro.repair_order_num_alias = mo.order_id
              left join zsf_user u on u.user_id=ro.worker_user_id
              where ro.pay_time>0 $where";

        $count = M()->query($countsql);

        $Page = new \Think\Page($count[0]['sum'], 25);
        $sql = "select ro.repair_order_num_alias,ro.pay_time,ro.worker_user_id,u.user_name,u.phone,ro.total-ro.diagnosis_cost total,mo.total ptotal,mo.money,mo.comment from zsf_repair_order ro
              left join zsf_rewards_money mo on ro.repair_order_num_alias = mo.order_id
              left join zsf_user u on u.user_id=ro.worker_user_id
              where ro.pay_time>0 $where limit " . $Page->firstRow . ',' . $Page->listRows;
        //存当前sql语句到redis，导出excel的时候需用
        $redis = S(array('type' => 'redis'));
        S( $this->sqlname, $sql);
        $list = M()->query($sql);

        $show = $Page->show();
        $this->assign('list', $list);
        $this->assign('page', $show);// 赋值分页输出
        $this->display();
    }


    /**
     * @throws \PHPExcel_Exception
     * 导出excel
     */
    public function excelout(){
        // phpExcel();
        //exit;
        /*    $sheetindex=array(
                'A'=>array('index'=>'A1','name'=>'订单编号','size'=>'20'),
                'B'=>array('index'=>'B1','name'=>'故障金额','size'=>'20'),
                'C'=>array('index'=>'C1','name'=>'优惠券','size'=>'20'),
                'D'=>array('index'=>'D1','name'=>'诊断费','size'=>'20'),
                'E'=>array('index'=>'E1','name'=>'小费','size'=>'20'),
                'F'=>array('index'=>'F1','name'=>'订单总金额(元)','size'=>'20'),
                'G'=>array('index'=>'G1','name'=>'支付时间','size'=>'20'),
            );    */
        $sheetindex=array(
            'A'=>array('index'=>'A1','name'=>'序号','size'=>'20'),
            'B'=>array('index'=>'B1','name'=>'订单编号','size'=>'20'),
            'C'=>array('index'=>'C1','name'=>'支付时间','size'=>'20'),
            'D'=>array('index'=>'D1','name'=>'师傅信息','size'=>'20'),
            'E'=>array('index'=>'E1','name'=>'订单金额(元)','size'=>'20'),
            'F'=>array('index'=>'F1','name'=>'订单星级','size'=>'20'),
            'G'=>array('index'=>'G1','name'=>'佣金(-)或奖励(+)','size'=>'20'),
            'H'=>array('index'=>'H1','name'=>'说明','size'=>'20'),
        );
        $title='师傅佣金和奖励管理';
        $type=1;
        $redis = S(array('type' => 'redis'));
        $sql=S($this->sqlname);
        $data = M()->query($sql);
        phpExcel($sheetindex,$data,$title,$type);
        exit;

    }
}